package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.andview.refreshview.XRefreshView;
import org.xutils.x;
import java.util.ArrayList;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.ShopDetailsActivity;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.FiltrateClassifyBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeAttentionBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.StatusBarUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.view.SwipteLayout2;
import a66weding.com.jiehuntong.widgets.NoScrollGridView;

/**
 * 我的—关注模块
 * Created by Administrator on 2016/7/7.
 */
public class MeAttentionActivity extends MeBaseActivity implements View.OnClickListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private LinearLayout mShowList;
    private LinearLayout mNoList;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private TextView mRightTitleTv;
    private ListView lv;
    private SwipteLayout2 mSwipLayout;
    private SwipteLayout2 mSwipLayout2;
    private String category = "";
    private ArrayList<MeAttentionBean.DataBean> meList = new ArrayList<MeAttentionBean.DataBean>();
    private MeAttentionBean attentionBean;
    private ArrayList<MeAttentionBean.DataBean.ProductCategoryListBean> list
            = new ArrayList<MeAttentionBean.DataBean.ProductCategoryListBean>();
    private ArrayList<FiltrateClassifyBean.DataBean> mFiltrateList = new ArrayList<FiltrateClassifyBean.DataBean>();
    private FiltrateClassifyBean filtrateClassifyBean;
    private PopupWindow mPopWindow;
    private RelativeLayout mRelaHeah;
    private XRefreshView xRefreshView;
    private int businessId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_attention);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("MeAttentionActivity", this);
        initview();
    }
    // 页面可见调用此方法
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
    /**
     * 页面初始化数据调用的方法
     */
    private void initData() {
        // 请求列表的网络请求
        String myUrl = UrlDealUtil.getRealUrlNewIdApi(RequestCode.GET_BUSINESSLISTBYMEMBERID);
        String url = myUrl + "&category=" + category;
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.GETREQUEST,
                url, this, MeAttentionBean.class, RequestCode.GET_BUSINESSLISTBYMEMBERID);
        HttpHandleUtil.executeCommonGet(MeAttentionActivity.this, httpRequestBean);
        // 请求筛选分类的请求
        String meUrl = RequestCode.GET_PRODUCTCATEGORYBYPID;
        HttpRequestBean httpRequestBeans = new HttpRequestBean(RequestCode.GETREQUEST,
                meUrl, this, FiltrateClassifyBean.class, RequestCode.GET_PRODUCTCATEGORYBYPID);
        HttpHandleUtil.executeCommonGet(this, httpRequestBeans);
    }

    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.lay_head_backlay);
        mShowList = (LinearLayout) findViewById(R.id.ll_me_show_attention);
        mNoList = (LinearLayout) findViewById(R.id.ll_me_no_attention);
        mBackContentTv = (TextView) findViewById(R.id.headleft_title_tv);
        mHeaderTitleTv = (TextView) findViewById(R.id.headcenter_title_tv);
        mRightTitleTv = (TextView) findViewById(R.id.headright_title_tv);
        lv = (ListView) findViewById(R.id.lv);
        mRelaHeah = (RelativeLayout) findViewById(R.id.rl_mypublic_heahview);
        initRefreshView();
        mBackContentTv.setText("我的");
        mHeaderTitleTv.setText("我的关注");
        mRightTitleTv.setText("筛选");
        mBackLay.setOnClickListener(this);
        mRightTitleTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_head_backlay: //点击我的，返回到上一级界面
                finish();
                break;
            case R.id.headright_title_tv: //点击筛选,根据分类进行选择
                if (mPopWindow != null) {
                    mPopWindow.dismiss();
                    mPopWindow = null;
                }
                showPopupWindow();
                break;
        }
    }

    //显示窗体
    private void showPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(MeAttentionActivity.this).inflate(R.layout.popupwindow_filtrate, null);
        mPopWindow = new PopupWindow(contentView,
                -1, -2, true);
        mPopWindow.setContentView(contentView);
        mPopWindow.setBackgroundDrawable(new ColorDrawable(
                Color.TRANSPARENT));
        //设置各个控件的点击响应
        ListView lv_popupwindow = (ListView) contentView.findViewById(R.id.lv_popupwindow);
        lv_popupwindow.setAdapter(new MeAdapter());
        lv_popupwindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                category = "" + mFiltrateList.get(position).getI_Id();
                onResume();
                if (mPopWindow != null) {
                    mPopWindow.dismiss();
                    mPopWindow = null;
                }
            }
        });
        //显示PopupWindow
        View rootview = LayoutInflater.from(MeAttentionActivity.this).inflate(R.layout.activity_main, null);
        mPopWindow.showAtLocation(rootview, Gravity.TOP, 0, mRelaHeah.getHeight() + StatusBarUtils.getStatusHeight(this));

    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        xRefreshView.stopRefresh();
        if (data instanceof MeAttentionBean) {
            attentionBean = (MeAttentionBean) data;
            meList = (ArrayList<MeAttentionBean.DataBean>) attentionBean.getData();
            if (meList.size() == 0) {
                mNoList.setVisibility(View.VISIBLE);
                mShowList.setVisibility(View.GONE);
            } else {
                mNoList.setVisibility(View.GONE);
                mShowList.setVisibility(View.VISIBLE);
                lv.setAdapter(new MyAdapter());
            }
        } else if (data instanceof CommonResultBean) {
            Toast.makeText(MeAttentionActivity.this, "取消关注成功", Toast.LENGTH_SHORT).show();
            onResume();
        } else if (data instanceof FiltrateClassifyBean) {
            filtrateClassifyBean = (FiltrateClassifyBean) data;
            mFiltrateList = (ArrayList<FiltrateClassifyBean.DataBean>) filtrateClassifyBean.getData();
            // 向集合中再添加一个元素
            FiltrateClassifyBean.DataBean classifyBean = new FiltrateClassifyBean.DataBean();
            classifyBean.setNvc_Name("全部");
            classifyBean.setI_Id(0);
            mFiltrateList.add(0, classifyBean);
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(MeAttentionActivity.this, "请求数据失败", Toast.LENGTH_SHORT).show();
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return meList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            final ViewHolder mHolder;
            if (convertView != null) {
                mHolder = (ViewHolder) convertView.getTag();
            } else {
                convertView = View.inflate(getApplicationContext(), R.layout.item_me_indent_attention, null);
                mHolder = new ViewHolder();
                mHolder.mSwipeLay = (SwipteLayout2) convertView.findViewById(R.id.sw_delete);
                mHolder.mLogIm = (ImageView) convertView.findViewById(R.id.img_indent);
                mHolder.mShopNameTv = (TextView) convertView.findViewById(R.id.tv_indent_code);
                mHolder.mShopSetMealTv = (TextView) convertView.findViewById(R.id.tv_me_set_meal);
                mHolder.mShopCaseTv = (TextView) convertView.findViewById(R.id.tv_me_case);
                mHolder.mShopFinsTv = (TextView) convertView.findViewById(R.id.tv_fans);
                mHolder.mDeleteBt = (Button) convertView.findViewById(R.id.btn_delete);
                mHolder.mAttentionLay = (LinearLayout) convertView.findViewById(R.id.item_attention_l);
                mHolder.mTypeGv = (NoScrollGridView) convertView.findViewById(R.id.gv_me_indent_attention);
                mSwipLayout = mHolder.mSwipeLay;
                convertView.setTag(mHolder);
            }
            mSwipLayout.setOnItemStateChanged(new SwipteLayout2.OnItemStateChanged() {
                @Override
                public void onOpen(SwipteLayout2 layout) {
                    mSwipLayout2 = layout;
                }

                @Override
                public void onClose(SwipteLayout2 layout) {
                    if (mSwipLayout2 == layout) {
                        mSwipLayout2 = null;
                    }
                }

                @Override
                public void onDown(SwipteLayout2 layout) {
                    if (mSwipLayout2 != null && mSwipLayout2 != layout) {
                        mSwipLayout2.closeMenu();
                    }
                }
            });

            // 注意:这里list和meList是两个不同的集合
            list = (ArrayList<MeAttentionBean.DataBean.ProductCategoryListBean>) meList.get(position).getProductCategoryList();
            x.image().bind(mHolder.mLogIm, meList.get(position).getNvc_Logo());
            mHolder.mShopNameTv.setText(meList.get(position).getNvc_StoreName());
            mHolder.mShopSetMealTv.setText("" + meList.get(position).getProductCount());
            mHolder.mShopCaseTv.setText("" + meList.get(position).getCasesCount());
            mHolder.mShopFinsTv.setText("" + meList.get(position).getBusinessMemberCount());
            mHolder.mTypeGv.setAdapter(new GvAdapter());
            mHolder.mDeleteBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取商家ID
                    int businessId = meList.get(position).getI_Id();
                    String myUrl = UrlDealUtil.getRealUrlNewIdApi(RequestCode.POST_CANCELBUSINESSMEMBER);
                    String url = myUrl + "&BusinessID=" + businessId;
                    HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                            MeAttentionActivity.this, CommonResultBean.class, RequestCode.POST_CANCELBUSINESSMEMBER);
                    HttpHandleUtil.executeCommonPost(MeAttentionActivity.this, httpRequestBean);
                }
            });
            // 进入店铺有问题,等接口无问题再开启
            mHolder.mAttentionLay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    businessId = meList.get(position).getI_Id();
                    Intent intent = new Intent();
                    intent.setClass(MeAttentionActivity.this, ShopDetailsActivity.class);
                    intent.putExtra("nvc_num", meList.get(position).getNvc_No());
                    intent.putExtra("BusinessID", businessId);
                    startActivity(intent);
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        SwipteLayout2 mSwipeLay;
        ImageView mLogIm;
        TextView mShopNameTv;
        TextView mShopSetMealTv;
        TextView mShopCaseTv;
        TextView mShopFinsTv;
        Button mDeleteBt;
        TextView tv_listview_popupwindow;
        LinearLayout mAttentionLay;
        a66weding.com.jiehuntong.widgets.NoScrollGridView mTypeGv;
        private LinearLayout lr;
    }

    class MeAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mFiltrateList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder holder;
            if (convertView != null) {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            } else {
                view = View.inflate(getApplicationContext(), R.layout.item_popupwindow_filtrate, null);
                holder = new ViewHolder();
                holder.tv_listview_popupwindow = (TextView) view.findViewById(R.id.tv_listview_popupwindow);
                view.setTag(holder);
            }
            holder.tv_listview_popupwindow.setText(mFiltrateList.get(position).getNvc_Name());
            return view;
        }
    }

    //初始化上啦刷新
    private void initRefreshView() {
        xRefreshView = (XRefreshView) findViewById(R.id.ll_me_show_attention);
        //是否允许下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);

        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore(boolean isSlience) {
            }
        });
    }

    class GvAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            GvHolder gvHolder;
            if (convertView != null) {
                view = convertView;
                gvHolder = (GvHolder) view.getTag();
            } else {
                view = View.inflate(getApplicationContext(), R.layout.item_me_indent_attention_classify, null);
                gvHolder = new GvHolder();
                gvHolder.mGvTv = (TextView) view.findViewById(R.id.tv_me_classify);
                view.setTag(gvHolder);
            }
            gvHolder.mGvTv.setText(list.get(position).getNvc_Name());
            return view;
        }
    }

    class GvHolder {
        TextView mGvTv;
    }
}
